import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  BeforeInsert
} from 'typeorm';
import * as bcrypt from 'bcryptjs';
import { Exclude } from 'class-transformer';
import { BaseEntity } from 'src/entities/base.entity';

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    type: 'varchar',
    length: 64,
    comment: '用户名',
  })
  username: string;

  @Column({
    type: 'varchar',
    length: 64,
    nullable: true,
    comment: '昵称',
  })
  nickname: string;

  @Column({
    type: 'char',
    length: 1,
    default: '1',
    nullable: true,
    comment: '性别（1：男，2：女,3:未知）',
  })
  gender: string;

  @Exclude()  //查询返回结果时隐藏此列
  @Column({
    type: 'varchar',
    length: 100,
    select:false,   //查询时隐藏此列
    nullable: true,
    comment: '密码',
  })
  password: string;

  @Column({
    type: 'int',
    nullable: true,
    comment: '部门ID',
  })
  dept_id: number;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '用户头像',
  })
  avatar: string;

  @Column({
    type: 'varchar',
    nullable: true,
    length: 20,
    comment: '联系方式',
  })
  mobile: string;

  @Column({
    default: '0',
    comment: '用户状态（0：正常，1：禁用）',
  })
  status: string;

  @Column({
    type: 'varchar',
    nullable: true,
    length: 128,
    comment: '用户邮箱',
  })
  email: string;

  @Column({
    type: 'simple-enum',
    enum: [0, 1],
    default: 0,
    comment: '逻辑删除标识（1：已删除，0：未删除）',
  })
  deleted: number;
  
  @BeforeInsert()
  async encryptPwd() {
    if (!this.password) return;
    this.password = await bcrypt.hashSync(this.password, 10);
  }
}
